import MatterBody from "./MatterBody";
import MatterManager from "../../managers/MatterManager";
import ConfigManager from "../../managers/ConfigManager";

export default class SnowBall extends MatterBody {
    /** @prop {name:radius, tips:"半径", type:Int, default:0}*/
    public radius: number = ConfigManager.initConfig.snowBallRadius;

    /** @prop {name:x, tips:"x坐标", type:Int, default:0}*/
    public x: number;
    /** @prop {name:x, tips:"y坐标", type:Int, default:0}*/
    public y: number;
    /** @prop {name:restitution, tips:"弹力系数", type:Int, default:0}*/
    public restitution: number;
    /** @prop {name:friction, tips:"摩擦力", type:Int, default:0}*/
    public friction: number;
    /** @prop {name:frictionStatic, tips:"静态摩擦力", type:Int, default:0}*/
    public frictionStatic: number;
    /** @prop {name:frictionAir, tips:"空气阻力", type:Int, default:0}*/
    public frictionAir: number;
    /** @prop {name:isStatic, tips:"是否是静止的", type:Bool, default:false}*/
    
    constructor() { super(); }
    
    public snowMan: any;
    public realOwner: Laya.Sprite;

    onEnable(): void {
        let owner = this.owner as Laya.Sprite;
        this.body = this.matter.Bodies.circle(owner.x, owner.y, this.radius, {
            isStatic: false,
            isSensor: true,
            friction: 1,
            frictionStatic: 1,
            frictionAir: 1,
            label: 'snowball'
        });
        this.realOwner = owner;

        super.onEnable();

    } 

    onUpdate() {
        super.onUpdate();

        let scale = this.body.circleRadius / this.radius;
        this.realOwner.scale(scale, scale);
    }


    
    public onCollision(event) {
        console.log('ball crash')

        let other = this.getCollisitionPair(event.pairs);
        if (other === this.snowMan) {
            return;
        }
        if (other.label === 'snowball' && other.circleRadius < this.body.circleRadius) {
            return;
        }

        let owner = this.owner as Laya.Sprite;
        owner.removeSelf();
        MatterManager.instance.removeFromWorld(this.body);
    }


    onDisable(): void {
        MatterManager.instance.removeFromWorld(this.body);
    }
}